package com.demo.myapplication.mvp.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.demo.myapplication.R;
import com.demo.myapplication.mvp.model.SettingInfo;
import com.demo.myapplication.mvp.model.UserInteractorImpl;
import com.demo.myapplication.mvp.presenter.UserPresenter;

public class MvpActivity extends AppCompatActivity implements UserView, View.OnClickListener {

  private UserPresenter presenter;
  private EditText mEtUsername;
  private EditText mEtPwd;
  private Button loginBtn;

  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mvp);
    mEtUsername = (EditText) findViewById(R.id.username);
    mEtPwd = (EditText) findViewById(R.id.password);
    loginBtn = (Button) findViewById(R.id.login);
    loginBtn.setOnClickListener(this);
    presenter = new UserPresenter(this, new UserInteractorImpl());

    SettingInfo.getInstance().init(this);
  }

  @Override
  protected void onResume() {
    super.onResume();
    presenter.resume();
  }

  @Override
  public void showUserAndPwd(String username, String pwd) {
    mEtUsername.setText(username);
    mEtPwd.setText(pwd);
  }

  @Override
  public void showLoginResult(String msg) {
    Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
  }

  @Override
  public void onClick(View v) {
    switch (v.getId()) {
      case R.id.login:
        presenter.login(mEtUsername.getText().toString(), mEtPwd.getText().toString());
        break;
      default:
        break;
    }
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    presenter.destroy();
  }
}
